﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.Model;
using rock.BLL;
using rock.UI.common;
using rock.common;

namespace rock.UI
{
    public partial class FormUserAccount : UserControl
    {
        DalUSER_ACCOUNT dal = new DalUSER_ACCOUNT();
        DalUSER_AUTHO dalAutho = new DalUSER_AUTHO();
        BllUSER_ACCOUNT bllUSER_ACCOUNT = new BllUSER_ACCOUNT();
        public FormUserAccount()
        {
            InitializeComponent();
            this.Dock =  System.Windows.Forms.DockStyle.Fill;

            this.userControlCommonListHeader1.addClick = addClick;
            this.userControlCommonListHeader1.updateClick = updateClick;
            this.userControlCommonListHeader1.deleteClick = deleteClick;
            this.userControlCommonListHeader1.searchClick = searchClick;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);

           
        }

        private void FormUserAccount_Load(object sender, EventArgs e)
        {
            refreshData();
        }
        //刷新数据
        public void refreshData()
        {

           // String username = this.toolStripTextBox1.TextBox.Text;
            String search = this.userControlCommonListHeader1.textBox1.Text;
            DataSet ds = dal.GetList(" USER_ACCOUNT.USERNAME LIKE '%" + search + "%' OR USER_ACCOUNT.ACCOUNT LIKE '%" + search + "%'");
            //int str = (int)ds.Tables[0].Rows[0]["FEE_AUTHO"];
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        

          


        private void addClick()
        {
            FormUserAccountUpdate formUau = new FormUserAccountUpdate();
            formUau.oneRefreshParentData = refreshData;
            formUau.ShowDialog(this);
        }
        private void updateClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                FormUserAccountUpdate formUau = new FormUserAccountUpdate();

                String account = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["ACCOUNT"].Value.ToString();

                ModelUSER_ACCOUNT accountModel = dal.GetModel(account);
                ModelUSER_AUTHO authoModel = dalAutho.GetModel(account);

                formUau._userAccount = accountModel;
                formUau._userAutho = authoModel;
                formUau.oneRefreshParentData = refreshData;
                formUau.ShowDialog(this);
            }
            else
            {
                MessageBox.Show("请选择要更新的记录.", "提示");
            }
        }
        private void deleteClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String account = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["ACCOUNT"].Value.ToString();

                if (account.Equals(Constant.SYSTEM_DEFAULT_ACCOUNT))
                {
                    MessageBox.Show(Constant.SYSTEM_DEFAULT_ACCOUNT + " 是系统内置帐号,不允许删除.", "提示");
                    return;
                }


                DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dialogResult.Equals(DialogResult.OK))
                {
                    bllUSER_ACCOUNT.deleteUser(account);
                    //dal.Delete(account);
                    //dalAutho.Delete(account);
                    //刷新数据
                    refreshData();
                }

            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        
        }

        
        private void searchClick()
        {
            
            refreshData();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.updateClick();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        
      
    }
}
